//
//  ZCAlbumSingerViewController.m
//  VoiceMusic
//
//  Created by dllo on 15/10/26.
//  Copyright © 2015年 zhangdeyu. All rights reserved.
//

#import "ZCAlbumSingerViewController.h"
#import <Masonry.h>
#import "ZCAFNetWork.h"
#import "ZCSingerAlbumModel.h"
#import "ZCPlayMusicTool.h"
#import <UIImageView+WebCache.h>
#import "ZCSingerAblumContentTableViewCell.h"
#import "ZCSingerContentModel.h"
@interface ZCAlbumSingerViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation ZCAlbumSingerViewController

- (void)dealloc
{
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    //返回键
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    leftButton.frame = CGRectMake(0, 0, 30, 30);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    [leftButton addTarget:self action:@selector(returnClick:) forControlEvents:UIControlEventTouchUpInside];
    _dataArray = [[NSMutableArray alloc] init];
    
    self.imageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 300)];
    [self.imageView setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:self.imageView];
    
    self.picturceImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    [self.imageView addSubview:self.picturceImageView];
    [self.picturceImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.imageView);
    }];
    self.picturceImageView.userInteractionEnabled = YES;
    
    self.picView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.imageView addSubview:self.picView];
    [self.picView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.imageView);
    }];
    self.picView.userInteractionEnabled = YES;
    [self.picView setBackgroundColor:[UIColor blackColor]];
    self.picView.alpha = 0.1;
    
    self.returnButtonBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.imageView addSubview:self.returnButtonBack];
    [self.returnButtonBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.imageView).offset(20);
        make.height.equalTo(@40);
        make.width.equalTo(@30);
    }];
//    [self.returnButtonBack addTarget:self action:@selector(returnButtonBackClick:) forControlEvents:UIControlEventTouchUpInside];
//    [self.returnButtonBack setImage:[UIImage imageNamed:@"fanhui.png"] forState:UIControlStateNormal];
    
    self.nameLable = [[UILabel alloc] initWithFrame:CGRectZero];
    [self.imageView addSubview:self.nameLable];
    [self.nameLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.returnButtonBack.mas_bottom).offset(40);
        make.left.equalTo(self.picView).offset(20);
        make.right.equalTo(self.picView).offset(0);
        make.height.equalTo(@40);
    }];
    [self.nameLable setFont:[UIFont boldSystemFontOfSize:26]];
    [self.nameLable setTextColor:[UIColor whiteColor]];
  
    self.album = [[UILabel alloc] initWithFrame:CGRectZero];
    [self.album setTextColor:[UIColor whiteColor]];
    [self.album setFont:[UIFont boldSystemFontOfSize:22]];
    [self.imageView addSubview:self.album];
    [self.album mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLable.mas_bottom).offset(40);
        make.left.equalTo(self.picView).offset(20);
        make.right.equalTo(self.picView).offset(0);
        make.height.equalTo(@30);
    }];
    
    [self creatViewNetWork];
    [self creatView];
    [self creatViewNetWorkMusic];
    
    
    // Do any additional setup after loading the view.
}
#pragma mark 返回
- (void)returnClick:(UIBarButtonItem *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)creatView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-49);
    }];
    [self.tableView setDataSource:self];
    [self.tableView setDelegate:self];
//    self.tableView.bounces = NO;
    self.tableView.rowHeight = 100;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView setBackgroundColor:[UIColor clearColor]];
    self.tableView.tableHeaderView = self.imageView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataArray count];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellID = @"cell";
    ZCSingerAblumContentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == NO) {
        cell = [[ZCSingerAblumContentTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
    }
    if (_dataArray.count > 0) {
        ZCSingerContentModel * pageModel = [_dataArray objectAtIndex:indexPath.row];
        [cell getInfo:pageModel];
    }
    return cell;
}

- (void)creatViewNetWork
{
    NSString *str = [NSString stringWithFormat:@"http://api.dongting.com/song/album/%@?agent=none&longitude=0.0&userId=0&utdid=UyKT11zHlGYBACzJ72FDxmdxJ&net=2&os=5.0.2&v=v8.1.1.2015081120&alf=700159&api_version=1.0&imei=863583022034533&latitude=0.0&f=168&user_id=0", self.str];
    NSLog(@"%@",str);
    [ZCAFNetWork getUrl:str body:nil result:ZCJSON headerFile:nil success:^(id result) {
        if (result == nil) {
            return;
        } else {
        NSMutableDictionary *array = [result objectForKey:@"data"];
        ZCSingerAlbumModel *singer = [[ZCSingerAlbumModel alloc] init];
        [singer setValuesForKeysWithDictionary:array];
        [self.picturceImageView sd_setImageWithURL:[NSURL URLWithString:singer.picUrl] placeholderImage:[UIImage imageNamed:@"4444"]];
        [self.nameLable setText:singer.name];
        [self.album setText:singer.singerName];
        [_tableView reloadData];
        }
    } failure:^(NSError *error) {
        
    }];
}

- (void)creatViewNetWorkMusic
{
    NSString *str = [NSString stringWithFormat:@"http://api.dongting.com/song/album/%@?agent=none&longitude=0.0&userId=0&utdid=UyKT11zHlGYBACzJ72FDxmdxJ&net=2&os=5.0.2&v=v8.1.1.2015081120&alf=700159&api_version=1.0&imei=863583022034533&latitude=0.0&f=168&user_id=0", self.str];
    NSLog(@"%@",str);
    [ZCAFNetWork getUrl:str body:nil result:ZCJSON headerFile:nil success:^(id result) {
        NSMutableDictionary *dic = [result objectForKey:@"data"];
        NSMutableArray *array = [dic objectForKey:@"songList"];
        for (NSMutableDictionary *diction in array) {
            ZCSingerContentModel *singerModel = [[ZCSingerContentModel alloc] init];
            [singerModel setValuesForKeysWithDictionary:diction];
            [_dataArray addObject:singerModel];
            NSLog(@"%lu", self.dataArray.count);
            [_tableView reloadData];
        }
    } failure:^(NSError *error) {
        
    }];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ZCSingerContentModel * model = [_dataArray objectAtIndex:indexPath.row];
    
    //    if(model.auditionList.count == 0) {
    //
    //        UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"消息" message:@"版权不支持" preferredStyle:UIAlertControllerStyleAlert];
    //        [self presentViewController:alter animated:YES completion:nil];
    //
    //        [self dismissViewControllerAnimated:YES completion:^{
    //        }];
    //        return;
    //    }
    if(model.mvList.count == 0) {
        
        UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"消息" message:@"版权不支持" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alter animated:YES completion:nil];
        
        [self dismissViewControllerAnimated:YES completion:^{
        }];
        return;
    }
    
    //判断歌曲是否重复点击
    if([self.lastStr isEqualToString:model.auditionList[0][@"url"]])return;
    
    self.lastStr = model.auditionList[0][@"url"];
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:model.name,@"name",model.singerName,@"nameSinge",model.mvList[0][@"picUrl"],@"heat",model.auditionList[0][@"url"],@"url",nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"play" object:self userInfo:dic];
    
    [[NSUserDefaults standardUserDefaults] setObject:model.auditionList[0][@"url"] forKey:@"url"];
    //播放音乐
    [ZCPlayMusicTool getPlay:model.auditionList[0][@"url"]];
    [ZCPlayMusicTool sharePlay].urlMusic = model.auditionList[0][@"url"];
    //发送监听列表的属性
    NSDictionary *arrDic = [NSDictionary dictionaryWithObjectsAndKeys:self.dataArray,@"arr", nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"arr" object:nil userInfo:arrDic];
    
    [ZCPlayMusicTool sharePlay].pross = @"0";
}



//- (void)returnButtonBackClick:(UIButton *)button
//{
//    [self.navigationController popViewControllerAnimated:YES];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
